﻿using System;
using System.Linq;
using System.Collections.Generic;
using PasswordControl.Core.Data;
using PasswordControl.UI.WPF.ViewModel.PasswordItem;
using System.Windows.Input;
using System.Windows.Data;

namespace PasswordControl.UI.WPF.ViewModel.PasswordPackage
{
    class PasswordPackageViewModel : ViewModelBase
    {
        private static readonly string ShowAllGroups = "All Groups";
        private static readonly string GeneralGroup = "General";

        private IPasswordPackage m_package;
        private string m_filter;
        private bool m_groupPasswords = false;
        private string m_selectedGroup;

        public ICommand AddPasswordCommand
        {
            get { return new AddPasswordCommand(this); }
        }

        public ListCollectionView Passwords
        {
            get 
            {
                var passwords = m_package.Passwords.AsQueryable();
                if (!string.IsNullOrEmpty(m_filter))
                {
                    passwords = passwords.Where(p => p.ItemName.Contains(m_filter));
                }
                if (!string.IsNullOrEmpty(SelectedGroup) && !SelectedGroup.Equals(ShowAllGroups))
                {
                    passwords = passwords.Where(p => p.Group == SelectedGroup);
                }
                var collection = new ListCollectionView(passwords.ToList());
                if (GroupPasswords) collection.GroupDescriptions.Add(new PropertyGroupDescription("Group"));
                return collection;
            }
        }

        public string Filter
        {
            get { return m_filter; }
            set { m_filter = value; OnPropertyChanged(o => (o as PasswordPackageViewModel).Passwords); }
        }

        public IEnumerable<string> Groups
        {
            get { return (new List<string> { ShowAllGroups }).Concat(m_package.Passwords.Select(p => p.Group).Distinct()); }
        }

        public bool GroupPasswords
        {
            get { return m_groupPasswords; }
            set 
            { 
                m_groupPasswords = value; 
                OnPropertyChanged(o => (o as PasswordPackageViewModel).Passwords);
                OnPropertyChanged(o => (o as PasswordPackageViewModel).Groups);
            }
        }

        public string SelectedGroup
        {
            private get { return m_selectedGroup; }
            set 
            { 
                m_selectedGroup = value; 
                OnPropertyChanged(o => (o as PasswordPackageViewModel).Passwords);
            }
        }

        public PasswordPackageViewModel(IPasswordPackage package)
        {
            m_package = package;
        }

        public void AddPassword()
        {
            var item = m_package.AddPasswordItem("[Name]");
            item.Group = (SelectedGroup != ShowAllGroups) ? SelectedGroup : GeneralGroup;
            OnPropertyChanged(o => (o as PasswordPackageViewModel).Passwords);
        }
    }
}
